package user

import (
	"golang.org/x/crypto/bcrypt"
)

type UserV1Service struct{}

func NewUserV1Service() *UserV1Service {
	return &UserV1Service{}
}

// GeneratePassword 生成加密后的密码
func (s *UserV1Service) GeneratePassword(password string) (string, error) {
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}
	return string(hashedPassword), nil
}

// CheckPassword 校验密码是否正确
func (s *UserV1Service) CheckPassword(hashedPassword, password string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
	return err == nil
}
